package com.yjhb.android.sbz.v2.activity

import android.graphics.Paint
import android.text.TextUtils
import android.view.View
import android.widget.TextView
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONObject
import com.yjhb.android.sbz.R
import com.yjhb.android.sbz.activity.BaseActivity
import com.yjhb.android.sbz.entity.Report
import com.yjhb.android.sbz.entity.Task
import com.yjhb.android.sbz.tool.Tool
import com.yjhb.android.sbz.v2.adapter.V2ReportContentAdapter
import com.yjhb.android.sbz.v2.entity.ReportStep
import com.yjhb.android.sbz.v2.mvp.presenter.V2ReportPresenter
import com.yjhb.android.sbz.v2.mvp.view.V2ReportView
import com.jojo.android.zxlib.tool.ZxLogTool
import com.jojo.android.zxlib.tool.ZxToast
import kotlinx.android.synthetic.main.activity_report_content_v3.*


/**
 *  创建者 zx
 *  创建时间 2021/4/7 9:34
 */
class ReportContentActivityV2 : BaseActivity<V2ReportPresenter>(), V2ReportView {

    private val adapter = V2ReportContentAdapter(this)
    private val data = mutableListOf<ReportStep>()
    private var report: Report? = null

    override fun initPresenter(): V2ReportPresenter? {
        return V2ReportPresenter(this)
    }

    override fun getLayoutId() = R.layout.activity_report_content_v3

    override fun initUI() {
        emptyRecyclerView.emptyView = emptyView
        emptyRecyclerView.adapter = adapter
        adapter.data = data
        report = intent.getSerializableExtra("report") as Report
        if (report == null) {
            ZxToast.showINFOBottomToast("数据异常！")
            return
        }
        report?.let {
            if (TextUtils.isEmpty(it.reason)) {
                reason_tv.visibility = View.GONE
            } else {
                reason_tv.visibility = View.VISIBLE
                reason_tv.text = it.reason
            }
        }
    }

    override fun initData() {
        report?.id?.let {
            presenter.prosecutionDetail(it)
        }
        report?.let {
            presenter.queryTaskByOrderId(it.bizId)
        }
    }

    override fun regEvent() {
        back.setOnClickListener { finish() }

    }

    override fun onFailure(code: Int, msg: String?) {
        ZxLogTool.e("msg", msg)
    }

    override fun onLoading() {
        loadingDialog.show()
    }

    override fun onLoadSuccess(data: List<JSONObject>) {
        this.data.clear()
        var step = ReportStep()
        step.avatar = report?.plaintiffAvatar
        step.content = JSON.parseObject(report?.plaintiffContent, ReportStep.DataBean::class.java)
        step.nickname = report?.plaintiffName
        step.state = 0
        step.time = report?.createTime
        this.data.add(step)
        if (report?.defendantContent!=null){
            step = ReportStep()
            step.avatar = report?.defendantAvatar
            step.state = 1
            step.content = JSON.parseObject(report?.defendantContent, ReportStep.DataBean::class.java)
            step.nickname = report?.defendantName
            step.time = report?.refuteTime
            this.data.add(step)
        }
        for (d in data) {
            step = ReportStep()
            step.avatar = report?.plaintiffAvatar
            step.content = JSON.parseObject(d.getString("fromContent"), ReportStep.DataBean::class.java)
            step.nickname = report?.plaintiffName
            step.state = 0
            step.time = d.getDate("fromUpdateTime")
            this.data.add(step)
            if (d.getString("toContent") != null) {
                step = ReportStep()
                step.avatar = report?.defendantAvatar
                step.state = 1
                step.content = JSON.parseObject(d.getString("toContent"), ReportStep.DataBean::class.java)
                step.nickname = report?.defendantName
                step.time = d.getDate("toUpdateTime")
                this.data.add(step)
            }

        }
        loadingDialog.cancel()
        adapter.notifyDataSetChanged()
    }

    override fun onTaskDetail(task: Task) {
        if (task != null) {
            task_title_ll.visibility = View.VISIBLE
            task_id_tv.visibility = View.VISIBLE
            task_title_tv.text = task.name
            task_title_tv.setOnClickListener { Tool.startPreviewTaskDetailActivity(activity, task.id) }
            title_right_tv.setOnClickListener { Tool.startPreviewTaskDetailActivity(activity, task.id) }
//            setTextViewUnderLine(task_title_tv)
            task_id_tv.text = "任务ID：" + task.id
        } else {
            task_title_ll.visibility = View.GONE
            task_id_tv.visibility = View.GONE
            title_right_tv.visibility = View.GONE
        }
    }


    /**
     * text 设置下划线
     * @param view
     */
    fun setTextViewUnderLine(view: TextView) {
        //方式1
//        view.paintFlags = view.paintFlags or Paint.UNDERLINE_TEXT_FLAG
        //方式2
        view.paint.flags = Paint.UNDERLINE_TEXT_FLAG
    }
}